/*
 * @description:
 * @version: 1.0.1
 * @Author: xuqiulin
 * @Date: 2021-04-28 21:20:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-04-29 11:47:15
 */
package main

import (
	"fmt"
	"log"
	"time"
	"xuqiulin/MyMongoDB/MongoDB1/mongodb"

	"github.com/globalsign/mgo/bson"
)

type Animal struct {
	Id   bson.ObjectId `bson:"_id" json:"id"`
	Name string        `bson:"Name" json:"Name"`
	Age  int64         `bson:"Age" json:"Age"`
	Time int64         `bson:"Time" json:"Time"`
}

func main() {
	mongodb.Init()

	cnt, err := mongodb.Count("server_4000", "Animal", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(cnt)
	// 插入数据
	err = mongodb.Insert("server_4000", "Animal",
		&Animal{Id: bson.NewObjectId(), Name: "小昆虫1", Age: 111, Time: time.Now().Unix()},
		&Animal{Id: bson.NewObjectId(), Name: "爱白菜2", Age: 222, Time: time.Now().Unix()},
	)
	if err != nil {
		log.Fatal(err)
	}

	result := Animal{}
	// 查询数据
	err = mongodb.FindOne("server_4000", "Animal", bson.M{"Name": "小昆虫1"}, nil, &result)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%+v\n", result)
}
